#include <avr/io.h>
#include <avr/delay.h>

uint16_t pressureChamberTemperature = 600;
uint16_t vacuumChamberTemperature = 0;
uint16_t pressureChamberPressure = 0;
uint16_t vacuumChamberPressure = 0;
uint16_t resistanceWireVoltagePressureChamber = 0;
uint16_t resistanceWireVoltageVacuumChamber = 0;
uint16_t battery1Voltage = 0;
uint16_t battery2Voltage = 0;

void initADC()
{
	DIDR0	|= (1 << ADC0D)|(1 << ADC1D)|(1 << ADC2D)|(1 << ADC3D)|(1 << ADC4D)|(1 << ADC5D);	//Disable digital input buffer
	ADCSRA	|= (1 << ADPS1)|(1 << ADPS2);		//64 Prescale (= 125 kHz)
	ADMUX	|= 1 << ADLAR;						//Left adjust the ADC result
	ADMUX	|= 1 << REFS0;						//Use AVcc as reference
	ADMUX   &= ~(1<<REFS1);
	//ADCSRA	|= 1 << ADIE;						//Enable interrupt
	ADCSRA	|= 1 << ADEN;						//Enable the ADC
}

uint16_t readADC()
{
	/*ch=ch&0b00001111;				//Select ADC Channel (ch)
	ADMUX|=ch;*/
	_delay_ms(10);
	ADCSRA|=(1<<ADSC);				//Start single conversion
	while (ADCSRA & (1<<ADSC)); 	//Wait for conversion to complete
	ADCSRA|=(1<<ADIF);				//Clear ADIF
	uint8_t theLow = ADCL;
	uint16_t tenBitValue = ADCH << 2 | theLow >> 6;
	return tenBitValue;
}

uint16_t getPressureChamberTemperature()
{
	//ADC0
	ADMUX &= ~(1<<MUX0);
	ADMUX &= ~(1<<MUX1);	
	ADMUX &= ~(1<<MUX2);
	ADMUX &= ~(1<<MUX3);
	
	pressureChamberTemperature = readADC();
	return pressureChamberTemperature;
}

uint16_t getVacuumChamberTemperature()
{
	//ADC1
	ADMUX |= (1<<MUX0);
	ADMUX &= ~(1<<MUX1);
	ADMUX &= ~(1<<MUX2);
	ADMUX &= ~(1<<MUX3);
	
	vacuumChamberTemperature = readADC();
	return vacuumChamberTemperature;
}

uint16_t getPressureChamberPressure()
{
	//ADC4
	ADMUX &= ~(1<<MUX0);
	ADMUX &= ~(1<<MUX1);
	ADMUX |= (1<<MUX2);
	ADMUX &= ~(1<<MUX3);
	
	pressureChamberPressure = readADC();
	return pressureChamberPressure;
}

uint16_t getVacuumChamberPressure()
{
	//ADC5
	ADMUX |= (1<<MUX0);
	ADMUX &= ~(1<<MUX1);
	ADMUX |= (1<<MUX2);
	ADMUX &= ~(1<<MUX3);
	
	vacuumChamberPressure = readADC();
	return vacuumChamberPressure;
}

uint16_t getResistanceWireVoltagePressureChamber()
{
	//ADC2
	ADMUX &= ~(1<<MUX0);
	ADMUX |= (1<<MUX1);
	ADMUX &= ~(1<<MUX2);
	ADMUX &= ~(1<<MUX3);
	
	resistanceWireVoltagePressureChamber = readADC();
	return resistanceWireVoltagePressureChamber;
}

uint16_t getResistanceWireVoltageVacuumChamber()
{
	//ADC3
	ADMUX |= (1<<MUX0);
	ADMUX |= (1<<MUX1);
	ADMUX &= ~(1<<MUX2);
	ADMUX &= ~(1<<MUX3);
	
	//Multiplexer ln 3
	PORTD &= ~(1<<PIND7);
	PORTB |= (1<<PINB0);
	PORTB &= ~(1<<PINB1);
	
	resistanceWireVoltageVacuumChamber = readADC();
	return resistanceWireVoltageVacuumChamber;
}

uint16_t getBattery1Voltage()
{
	//ADC3
	ADMUX |= (1<<MUX0);
	ADMUX |= (1<<MUX1);
	ADMUX &= ~(1<<MUX2);
	ADMUX &= ~(1<<MUX3);	
	
	//Multiplexer ln 1
	PORTD &= ~(1<<PIND7);
	PORTB &= ~(1<<PINB0);
	PORTB &= ~(1<<PINB1);
	
	battery1Voltage = readADC();
	return battery1Voltage;
}

uint16_t getBattery2Voltage()
{
	//ADC3
	ADMUX |= (1<<MUX0);
	ADMUX |= (1<<MUX1);
	ADMUX &= ~(1<<MUX2);
	ADMUX &= ~(1<<MUX3);	
	
	//Multiplexer ln 2
	PORTD |= (1<<PIND7);
	PORTB &= ~(1<<PINB0);
	PORTB &= ~(1<<PINB1);
	
	battery2Voltage = readADC();
	return battery2Voltage;
}
